// pages/time-records/time-records.js
const app = getApp()

Page({
  data: {
    records: [],
    loading: true,
    page: 1,
    limit: 10,
    hasMore: true
  },

  onLoad: function (options) {
    this.loadTimeRecords()
  },

  onShow: function () {
    this.loadTimeRecords()
  },

  onPullDownRefresh: function () {
    this.refreshRecords()
  },

  onReachBottom: function () {
    if (this.data.hasMore && !this.data.loading) {
      this.loadMore()
    }
  },

  loadTimeRecords: function () {
    if (this.data.loading) return

    this.setData({ loading: true })

    app.request({
      url: '/getTimeRecords',
      method: 'GET'
    }).then((res) => {
      if (res.code === 1) {
        const newRecords = res.data || []
        
        this.setData({
          records: newRecords,
          hasMore: false
        })
      }
    }).catch((err) => {
      console.error('加载计时记录失败:', err)
    }).finally(() => {
      this.setData({ loading: false })
      wx.stopPullDownRefresh()
    })
  },

  refreshRecords: function () {
    this.setData({
      page: 1,
      hasMore: true
    })
    this.loadTimeRecords()
  },

  loadMore: function () {
    if (this.data.hasMore && !this.data.loading) {
      this.setData({
        page: this.data.page + 1
      })
      this.loadTimeRecords()
    }
  }
})
